package com.itfenghuang.day07;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeMap;

public class A00_自然排序和比较器排序 {

    public static void main(String[] args) {
        TreeMap<Student1, String> map = new TreeMap<>(new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                int i = o1.getAge() - o2.getAge();
                int i1 = i == 0 ? o1.getName().compareTo(o2.getName()):i;
                return i1;
            }
        });
        map.put(new Student1("小小",48),"中国");
        map.put(new Student1("小美",18),"美国");
        map.put(new Student1("小英",26),"英国");
        map.put(new Student1("小鱼",26),"英国");
        System.out.println(map);



    }
}
class Student1 implements Comparable<Student1>{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student1() {
    }

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return age == student1.age &&
                Objects.equals(name, student1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student1 o) {
        int i=this.age-o.age;
        int i1 = i == 0 ? this.name.compareTo(o.name):i;
        return i1;
    }
}